<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8" >
            <a-form-model-item label="户名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusName">
              <j-popup
                v-model="model.cusName"
                 field="cusName"
                org-fields="id,name"
                dest-fields="customerId,cusName"
                code="arc_customer"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="抄表月份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readMonth">
              <a-input v-model="model.readMonth" placeholder="请输入抄表月份" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="起度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginScale">
              <a-input-number v-model="model.beginScale" placeholder="请输入起度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="止度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endScale">
              <a-input-number v-model="model.endScale" placeholder="请输入止度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="抄见水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readAmount">
              <a-input-number v-model="model.readAmount" placeholder="请输入抄见水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="抄见增减水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readAjustedAmount">
              <a-input-number v-model="model.readAjustedAmount" placeholder="请输入抄见增减水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="总分表分摊水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shareAmount">
              <a-input-number v-model="model.shareAmount" placeholder="请输入总分表分摊水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="换表读数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="replaceMeterScale">
              <a-input-number v-model="model.replaceMeterScale" placeholder="请输入换表读数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="换表水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="replaceAmount">
              <a-input-number v-model="model.replaceAmount" placeholder="请输入换表水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="实用水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="usedAmount">
              <a-input-number v-model="model.usedAmount" placeholder="请输入实用水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="计量增减水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="measureAjustedAmount">
              <a-input-number v-model="model.measureAjustedAmount" placeholder="请输入计量增减水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="计量水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="measureAmount">
              <a-input-number v-model="model.measureAmount" placeholder="请输入计量水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="预存增减水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storedAjustedAmount">
              <a-input-number v-model="model.storedAjustedAmount" placeholder="请输入预存增减水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="计费增减水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ajustedAmount">
              <a-input-number v-model="model.ajustedAmount" placeholder="请输入计费增减水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="计费水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number v-model="model.amount" placeholder="请输入计费水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="抄表本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bookId">
              <j-dict-select-tag type="list" v-model="model.bookId" dictCode="mws_t_mr_books,name,id" placeholder="请选择抄表本" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="抄表员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readerId">
              <j-select-user-by-dep v-model="model.readerId" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="水表运行状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="waterUsedType">
              <j-dict-select-tag type="list" v-model="model.waterUsedType" dictCode="water_used_type" placeholder="请选择水表运行状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="是否临时抄表" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isTemp">
              <j-dict-select-tag type="list" v-model="model.isTemp" dictCode="yn" placeholder="请选择是否临时抄表" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入说明" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="用水计量明细" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="msSubMeasuresTable.loading"
          :columns="msSubMeasuresTable.columns"
          :dataSource="msSubMeasuresTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MsMeasuresForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           readMonth: [
              { required: true, message: '请输入抄表月份!'},
           ],
           beginScale: [
              { required: true, message: '请输入起度!'},
           ],
           endScale: [
              { required: true, message: '请输入止度!'},
           ],
           readAmount: [
              { required: true, message: '请输入抄见水量!'},
           ],
           readAjustedAmount: [
              { required: true, message: '请输入抄见增减水量!'},
           ],
           shareAmount: [
              { required: true, message: '请输入总分表分摊水量!'},
           ],
           replaceMeterScale: [
              { required: true, message: '请输入换表读数!'},
           ],
           replaceAmount: [
              { required: true, message: '请输入换表水量!'},
           ],
           usedAmount: [
              { required: true, message: '请输入实用水量!'},
           ],
           measureAjustedAmount: [
              { required: true, message: '请输入计量增减水量!'},
           ],
           measureAmount: [
              { required: true, message: '请输入计量水量!'},
           ],
           storedAjustedAmount: [
              { required: true, message: '请输入预存增减水量!'},
           ],
           ajustedAmount: [
              { required: true, message: '请输入计费增减水量!'},
           ],
           amount: [
              { required: true, message: '请输入计费水量!'},
           ],
           bookId: [
              { required: true, message: '请输入抄表本!'},
           ],
           readerId: [
              { required: true, message: '请输入抄表员!'},
           ],
           waterUsedType: [
              { required: true, message: '请输入水表运行状态!'},
           ],
           isTemp: [
              { required: true, message: '请输入是否临时抄表!'},
           ],
        },
        refKeys: ['msSubMeasures', ],
        tableKeys:['msSubMeasures', ],
        activeKey: 'msSubMeasures',
        // 用水计量明细
        msSubMeasuresTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '用水性质',
              key: 'natureId',
              type: FormTypes.sel_search,
              dictCode:"mws_t_base_natures,name,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '阶梯号',
              key: 'tierNo',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '计费水量',
              key: 'expenseAmount',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '说明',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/mr/msMeasures/add",
          edit: "/mr/msMeasures/edit",
          queryById: "/mr/msMeasures/queryById",
          msSubMeasures: {
            list: '/mr/msMeasures/queryMsSubMeasuresByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.msSubMeasuresTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.msSubMeasures.list, params, this.msSubMeasuresTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          msSubMeasuresList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(value,row){
       this.model = Object.assign(this.model, row);
     },

    }
  }
</script>

<style scoped>
</style>